package com.onlineMIS.ORM.entity.headQ.finance;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.ExcelTemplate;
import com.onlineMIS.common.loggerLocal;

//financeBillTask
public class ChainAcctFlowTemplate extends ExcelTemplate {
	
	public static final DecimalFormat df = new DecimalFormat("#.00");

	private final int date_row_ind = 1;
	private final int data_row_ind = 5;

	private final int chain_column = 0;
	private final int sales_q_column = 1;

	private List<ChainAcctFlowReportItem> list = new ArrayList<ChainAcctFlowReportItem>();
	private Date startDate;
	private Date endDate;
	private String chainName;

	public ChainAcctFlowTemplate(Date startDate, Date endDate, String chainName, String templateWorkbookPath,
		List<ChainAcctFlowReportItem> list) throws IOException {
		super(templateWorkbookPath);
		this.startDate = startDate;
		this.endDate = endDate;
		this.chainName = chainName;
		this.list = list;

	}

	public HSSFWorkbook process() {
		HSSFSheet sheet = templateWorkbook.getSheetAt(0);

		// 写报表头信息
		String startDate = Common_util.dateFormat.format(this.startDate);
		String endDate = Common_util.dateFormat.format(this.endDate);

		Row headerRow2 = sheet.getRow(1);
		loggerLocal.info(this.chainName + "店铺名称");
		if (this.chainName == null || this.chainName.length() == 0) {
			this.chainName = "所有连锁店";
		}

		headerRow2.createCell(1).setCellValue(this.chainName);

		Row headerRow3 = sheet.getRow(2);
		headerRow3.createCell(1).setCellValue(startDate);

		Row headerRow4 = sheet.getRow(3);
		headerRow4.createCell(1).setCellValue(endDate);

		for (int i = 0; i < list.size(); i++) {
			ChainAcctFlowReportItem item = list.get(i);
			Row row = sheet.createRow(i + data_row_ind);
			if (item.getCust() != null) {
				row.createCell(0).setCellValue(item.getCust().getName());// 店铺名称
			} else {
				row.createCell(0).setCellValue("");// 店铺名称
			}
			row.createCell(1).setCellValue(Common_util.dateFormat_f.format(item.getDate())); // 日期
			row.createCell(2).setCellValue(item.getItemTypeName()); // 单据种类
			row.createCell(3).setCellValue(Integer.toString(item.getQuantity())); // 单据数量
			row.createCell(4).setCellValue(Double.toString(item.getAmount())); // 金额
			row.createCell(5).setCellValue(Double.toString(item.getAcctIncrease())); // 应收增加
			row.createCell(6).setCellValue(Double.toString(item.getAcctDecrease()));// 减少
			row.createCell(7).setCellValue(Common_util.getDecimalDouble(item.getPostAcct()));// 减少 item.getPostAcct()
			row.createCell(8).setCellValue(item.getComment());// 减少
		}
		return templateWorkbook;
	}

}
